### Emacs shell scripts mainly for daemons

function emacs-session () {
    if [ -z $1 ]; then
        SESSION="${EMACS_SESSION:-default}"
    else
        SESSION="$1"
    fi
    emacs --daemon="${SESSION}"
}

function emacs-list-sessions () {
    ps aux | grep -i emacs | grep daemon | awk -F= '{ print $NF }'
}

function emacs-stop () {
    if [ -z $1 ]; then
        SESSION="${EMACS_SESSION:-default}"
    else
        SESSION="$1"
    fi
    emacsclient -s "${SESSION}" -e "(save-buffers-kill-emacs)"
}

function emacs-kill () {
    if [ -z $1 ]; then
        SESSION="${EMACS_SESSION:-default}"
    else
        SESSION="$1"
    fi
    emacsclient -s "${SESSION}" -e "(kill-emacs)"
}

function emacs-edit () {
    echo "TODO"
    # emacsclient -c -n -s "${SESSION}"
    # [ -e $1 ] or [ $1 in emacs-list-sessions ]
    # who is in the higher priority
}

function emacs-view () {
    echo "TODO"
    # emacsclient -c -n -s ${SESSION} -e "(setq buffer-read-only t)"
    # same as emacs-edit
}
